package task.bean;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.mapper.AutoTradeMapper;
import com.mapper.TradeMapper;
import com.mapper.UsersMapper;
import com.pojo.OUser;
import com.pojo.platform;
import com.service.AutoTradeService;
import com.service.WtdService;
import com.serviceImpl.WtdServiceImpl;
//@Scheduled(cron = "0/5 * * * * ? ")
@Component
public class AutoInsertData {
	
	@Autowired
	private AutoTradeService autoTradeService;
	@Resource
	//private org.mybatis.spring.SqlSessionFactoryBean sqlSessionFactory;
    private AutoTradeMapper autoTradeMapper;
	@Resource
	private WtdService wtdService;
	@Resource
	private UsersMapper usersMapper;
	
	public AutoInsertData() {
		//System.out.println("AutoInsertData:"+sqlSessionFactory);
	}


	//真的商品调用该方法进行自动交易
	//@Scheduled(cron = "0 0/2 * * * ? ")
	//@Scheduled(cron = "0/5 * * * * ? ")
	public void insertData() {
		if (check())
			exec();
	}
	//假的商品调用该方法进行自动交易
	public void insertAutoData(String goodid) {
		String UserID1=autoTradeMapper.selectUser();//搜索虚拟帐号,只插入成交记录和曲线记录
		String UserID2=autoTradeMapper.selectUser();//买家虚拟帐号,只插入成交记录和曲线记录
			autoexec(goodid, UserID1, UserID2);
	}
	public boolean check() {
		// 1虚拟挂单是否开启
		// 2如果未开启退出
		// 3取出有委托单的数量
		// 4如果有委托单数量，退出 false
		// return true
			try {
				int max=60000;
		        int min=10000;
		        Random random = new Random();
		        int s = random.nextInt(max)%(max-min+1) + min;
				
				String str = String.valueOf(s);
		        //取减一次方
		        int m = (int) Math.pow(10, str.length()-1);
		        //利用int会去掉小数。
		        System.out.println(s/m *m);
		        int aaa= s/m *m;
		        //Thread.sleep(aaa);
				platform pt = autoTradeMapper.selectplat();//检测platform所有的信息
				//System.out.println("!!!!!!!!!!!!!!!!!!!"+pt);

				if (pt.getAutostate() == 1) {//autostate为1时，自动插入数据开启
					//System.out.println("这好似"+autoTradeMapper.selectnum().size());
					if (autoTradeMapper.selectnum().size()!=0) {
						
					
						return false;
						
					}
				
					return  true;
				} 
				//System.out.println("2");
					return false;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}
			return true;
	}
	//假的
	private void autoexec(String goodid, String UserID1, String UserID2) {
		// 4以随机价插入卖方成交记录和买方成交记录
		platform pt = autoTradeMapper.selectplat();// 搜索platform表的所有信息
		BigDecimal open = autoTradeMapper.tradestatis();// 搜索开盘价
		int openprice = open.intValue();
		BigDecimal b1 = new BigDecimal("100");
		BigDecimal b2 = new BigDecimal("1");
		int max = openprice * pt.getMaxtate() / 100;
		int min = openprice * pt.getMinrate() / 100;
		Random random = new Random();
		int average = random.nextInt(max) % (max - min + 1) + min;

		// 抽取随机帐号，状态为1 ，库存大于0作为卖房
		// 抽取随机帐号，状态为1 ，库存等于0的作为买方
		HashMap<String, Object> trade = new HashMap<String, Object>();
		trade.put("goodid", goodid);
		trade.put("UserID1", UserID1);
		trade.put("UserID2", UserID2);
		autoTradeMapper.autoTradeMapper(trade);
	}
	//真的
	private void exec() {
		int x=3;
		System.out.println(x);
		// 1先取出最高最低百分比
		// 2把百分比x开盘价得出最高最低价
		// 3去最高最低价中的随机价
		// 4以随机价插入卖方成交记录和买方成交记录
		//以每一天最近一次成交价格为最低价，到每天的最高价之间随即震荡
		platform pt = autoTradeMapper.selectplat();// 搜索platform表的所有信息
		
		BigDecimal open = autoTradeMapper.selectlast();// 搜索开盘价
		BigDecimal open1=autoTradeMapper.tradestatis();
		
		int openprice1 = open1.intValue();
		if(open==null) {
			open=autoTradeMapper.tradestatis();
		}
		System.out.println("开盘价"+open);
		BigDecimal last = autoTradeMapper.selectlast();//最新成交价格
		System.out.println("最新成交价格价"+last);
		int openprice = open.intValue();
		System.out.println("开盘价"+openprice);
		if(last==null) {
			int xian = openprice+openprice*x/100;
			System.out.println(xian);
			int max = xian+xian * pt.getMaxtate() / 100;
			int min = xian-xian * pt.getMinrate() / 100;//max和min是上下限值
			Random random = new Random();
			int average = random.nextInt(max) % (max - min + 1) + min;
			BigDecimal single = new BigDecimal(Integer.toString(average));//单价
			System.out.println("单价"+single);
			int a = random.nextInt(5)+1;//去1~5的随机数
			int b = a*average;
			BigDecimal total = new BigDecimal(Integer.toString(b));
			System.out.println("总价"+total);//取总价

			Date now = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time = dateFormat.format(now);//取当前时间
			HashMap<String, Object> autoaaa = new HashMap<String, Object>();
				autoaaa.put("obv", a);
				autoaaa.put("zpost", total);
				autoaaa.put("wprice", single);
				autoaaa.put("ttime", time);
				autoaaa.put("openprice", openprice);
				autoTradeService.insertseletion(autoaaa);
		}
		if(last!=null) {
			int start = last.intValue();//最新成交价格
			System.out.println("start"+start);
			int xian = openprice1+openprice1*x/100;//设值震荡的限值
			System.out.println("限值"+xian);
			int max = xian+xian * pt.getMaxtate() / 100;
			int min = xian-xian * pt.getMinrate() / 100;
			System.out.println(max);
			System.out.println(min);
			Random random = new Random();
			int average = (int)(min+Math.random()*(max-min+1));
			System.out.println("随机数"+average);

			
			BigDecimal single = new BigDecimal(Integer.toString(average));//单价
			System.out.println("单价"+single);
			int a = random.nextInt(5)+1;
			int b = a*average;
			BigDecimal total = new BigDecimal(Integer.toString(b));
			System.out.println("总价"+total);

			Date now = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time = dateFormat.format(now);//取当前时间
			HashMap<String, Object> autoaaa = new HashMap<String, Object>();
				autoaaa.put("obv", a);
				autoaaa.put("zpost", total);
				autoaaa.put("wprice", single);
				autoaaa.put("ttime", time);
				autoaaa.put("openprice", last);
				autoTradeService.insertseletion(autoaaa);
		}
		}
	//天（星期）（1~7 1=SUN 或 SUN，MON，TUE，WED，THU，FRI，SAT）
	//原来写的是cron ="0 0 23 ? * MON-FRI"
	@Scheduled(cron ="0 0 23 ? * 1-7")
	public void insertha() {
		wtdService.qcwtd();
	}//周一至周五的上午10:15触发 
	
	@Scheduled(cron ="0 20 23 ? * 1-7")
	public void excerefor() {
		usersMapper.excerefor();
	}
}
